package com.tfjybj.itoo.exam.provider.controller;

import com.dmsdbj.itoo.tool.business.ItooResult;
import com.dmsdbj.itoo.tool.constantutil.YearSemester;
import com.github.pagehelper.PageInfo;
import com.tfjybj.itoo.exam.entity.AssessmentAdviceEntity;
import com.tfjybj.itoo.exam.model.EvaluationRecordModel;
import com.tfjybj.itoo.exam.provider.service.AssessmentAdviceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * AssessmentAdviceController
 * trAssessmentAdvice表
 *
 * @author 杨江涛
 * @version 2.0.0
 * @since 2.0.0 2018-12-18 18:19:48
 */
@Api(tags = {"AssessmentAdvice表接口"})
@RequestMapping(value = "/AssessmentAdvice")
@RestController
public class AssessmentAdviceController {

    @Resource
    private AssessmentAdviceService assessmentAdviceService;

    //region 模板生成：基本增删改

    /**
     * 添加或更新问卷答题记录
     * @param model EvaluationRecordModel 问卷记录实体
     * @return 添加的结果
     * @author 杨晓慧
     * @since 2.0.0 2018年12月26日14:52:09
     */
    @ApiOperation(value = "添加或更新问卷答题记录")
    @PostMapping(value = {"/insertOrUpdateAssessmentAdvice"})
    public ItooResult insertOrUpdateAssessmentAdvice(@RequestBody EvaluationRecordModel model) {
        AssessmentAdviceEntity assessmentAdviceEntity = new AssessmentAdviceEntity();
        BeanUtils.copyProperties(model, assessmentAdviceEntity);
        List<String> yearSemesterCode = YearSemester.getYearSemesterCode(0, 0);
        assessmentAdviceEntity.setSchoolYear(yearSemesterCode.get(0));
        assessmentAdviceEntity.setQuestionMainId(model.getEvaluationMainId());
        return assessmentAdviceService.insertOrUpdateAssessmentAdvice(assessmentAdviceEntity);
    }

     /**
     * 根据id查找TrAssessmentAdvice
     * @param paperId 问卷id
     * @return 根据问卷id
     * @author 杨晓慧
     * @since 2.0.0 2018年12月26日20:10:07
     */
    @ApiOperation(value = "根据paperId,courseId,schoolYear查询简答题题干")
    @GetMapping(value = {"/queryQuestionMainByPaperId/{paperId}/{courseId}/{schoolYear}"})
    public ItooResult queryQuestionMainByPaperId(@ApiParam(value = "问卷id", required = true) @PathVariable String paperId,
                               @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
                               @ApiParam(value = "学年学期", required = true) @PathVariable String schoolYear) {
        return assessmentAdviceService.queryQuestionMainByPaperId(paperId, courseId, schoolYear);
    }

    /**
     * 根据id查找TrAssessmentAdvice
     * @param paperId 问卷id
     * @return 根据问卷id
     * @author 杨晓慧
     * @since 2.0.0 2018年12月26日20:10:07
     */
    @ApiOperation(value = "根据paperId,questionMainId,courseId,schoolYear查询简答题题干")
    @GetMapping(value = {"/queryAdviceDetailByPaperId/{paperId}/{questionMainId}/{courseId}/{schoolYear}/{page}/{pageSize}"})
    public ItooResult queryAdviceDetailByPaperId(@ApiParam(value = "问卷id", required = true) @PathVariable String paperId,
                                                 @ApiParam(value = "简答题题干id", required = true) @PathVariable String questionMainId,
                                                 @ApiParam(value = "课程id", required = true) @PathVariable String courseId,
                                                 @ApiParam(value = "学年学期", required = true) @PathVariable String schoolYear,
                                                 @ApiParam(value = "页码", required = true, example = "1")@PathVariable Integer page,
                                                 @ApiParam(value = "页数", required = true, example = "10") @PathVariable Integer pageSize) {
        return assessmentAdviceService.queryAdviceDetailByPaperId(paperId,questionMainId, courseId, schoolYear, page,pageSize);
    }

    /**
     * 分页查询所有TrAssessmentAdvice
     *
     * @param pageNo   页码
     * @param pageSize 每页条数
     * @return 分页查询的结果
     * @author 杨江涛
     * @since 2.0.0 2018-12-18 18:19:48
     */
    @ApiOperation(value = "分页查询所有TrAssessmentAdvice")
    @GetMapping(value = {"/queryPageAll/{pageNo}/{pageSize}"})
    public ItooResult queryPageAll(@ApiParam(name = "pageNo",value = "页码",required = true,example = "1")@PathVariable Integer pageNo,
								   @ApiParam(name = "pageSize",value = "页数",required = true,example = "10")@PathVariable Integer pageSize) {
        PageInfo<AssessmentAdviceEntity> trAssessmentAdvices = assessmentAdviceService.queryPageAll(pageNo, pageSize);
        return ItooResult.build(ItooResult.SUCCESS, "查询成功", trAssessmentAdvices);
    }
	
    //endregion

    /* **********************************以下为非模板生成的内容********************************* */

    /**
     * 实时更新评教建议记录到数据库和redis,即问卷简答题
     * @param model EvaluationRecordModel 问卷记录实体
     * @return
     * @author 魏恩君
     * @since 2.0.0 2019年3月21日17:01:45
     */
    @ApiOperation(value = "实时更新评教建议记录到数据库和redis,即问卷简答题")
    @PostMapping(value = {"/updateAdviceRecord"})
    public ItooResult updateAdviceRecord(@RequestBody EvaluationRecordModel model) {
        if (StringUtils.isEmpty(model.getPaperId())) {
            return ItooResult.build(ItooResult.FAIL, "问卷id为空");
        }
        if (StringUtils.isEmpty(model.getStudentId())) {
            return ItooResult.build(ItooResult.FAIL, "学生id为空");
        }
        if (StringUtils.isEmpty(model.getCourseId())) {
            return ItooResult.build(ItooResult.FAIL, "课程id为空");
        }
        if (StringUtils.isEmpty(model.getEvaluationMainId())) {
            return ItooResult.build(ItooResult.FAIL, "题干id为空");
        }
        if (StringUtils.isEmpty(model.getSchoolYearId())){
            return ItooResult.build(ItooResult.FAIL,"学年学期id为空");
        }
        Boolean flag = assessmentAdviceService.updateAdviceRecord(model);
        if(flag == true){
            return ItooResult.build(ItooResult.SUCCESS,"实时更新评教建议成功");
        }else{
            return ItooResult.build(ItooResult.FAIL,"实时更新评教建议失败");
        }

    }
   
}    
